package com.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.util.JdbcUtil;

/**
 * Servlet implementation class TypeAddServlet
 */
@WebServlet("/TypeAddServlet")
public class TypeAddServlet extends HttpServlet{

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String pname = request.getParameter("pname");
		Integer pprice = Integer.parseInt(request.getParameter("pprice")) ;
		Integer kid = Integer.parseInt(request.getParameter("kid")) ;
		String pimage = request.getParameter("pimage");
		String pdesc = request.getParameter("pdesc");
		try{
		JdbcUtil jdbc = new JdbcUtil();
		String sql = "insert into product(pname,pprice,kid,pimage,pdesc) values(?,?,?,?,?)";
		
		Object[] params = new Object[]{pname,pprice,kid,pimage,pdesc};
		jdbc.updatePreparedStatement(sql, params);
		jdbc.close();
		request.setAttribute("params", params);		
		request.getRequestDispatcher("/product?action=query").forward(request, response);
		}catch(Exception e){
			e.printStackTrace();
			request.setAttribute("mess", "新增失败，请稍后重试");
			request.getRequestDispatcher("inserterror.jsp").forward(request, response);
		}

		
	}
}
       
  


